package com.orion.async;

import com.orion.util.SmallTool;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * 异步任务，默认的线程池大小是8，必须加上@EnableAsync注解
 * <p>
 * 在自身的类中调用异步方法
 *
 * 是不生效的，涉及到spring的动态代理
 *
 * @author Administrator
 */
@Component
@ConditionalOnProperty(value = "async.asyncTaskInSelf", havingValue = "true")
public class AsyncTaskInSelf implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        SmallTool.printTimeAndThread("run async task in self class");
        customFutureTask();
    }

    /**
     * 只打印这一句，后续没反应
     * <p>
     * 1644740340334	|	1	|	main	|	run async task in self class
     */

    @Async
    public Future<String> customFutureTask() {
        String result = doTask();
        return AsyncResult.forValue(result);
    }


    public String doTask() {
        SmallTool.sleepMillis(5 * 1000);
        return "do Task";
    }
}
